const idCard = require('idcard'); // 身份证解析库
const lunarCalendarFix = require('lunar-calendar-fix'); // 通过公厉获取农历

/**
 * @summary 身份证信息解析
 * @description 根据身份证号解析基本信息、包括省市区
 * @param {string}  cardNo.query.required  - 身份证号码
 */

module.exports = (req, res) => {
  let { cardNo } = req.query;
  if (!cardNo) {
    res.api_error('请传入参数', 400, null);
    return;
  }
  if (cardNo.length !== 18) {
    res.api_error('身份证格式错误', 400, cardNo);
    return;
  }
  let cardInfo = idCard.info(cardNo); // 获取身份信息
  if (cardInfo && cardInfo.valid) {
    let birthday = cardInfo.birthday;
    const reg = /(\d{4})(\d{2})(\d{2})/;
    reg.test(birthday);
    const data = lunarCalendarFix.solarToLunar(RegExp.$1, RegExp.$2, RegExp.$3);
    cardInfo.zodiac = data.zodiac; // 生肖
    cardInfo.GanZhiYear = data.GanZhiYear; // 干支纪年
    cardInfo.GanZhiDay = data.GanZhiDay; // 干支纪月
    cardInfo.worktime = data.worktime; // 0 特殊安排  1 工作   2 放假
    cardInfo.nongli = `农历${data.lunarMonthName}${data.lunarDayName}日`;
  }
  res.api_success(cardInfo);
};
